package com.topboya.linker.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.topboya.linker.R;
import com.topboya.linker.diyview.group.GroupRecyclerAdapter;
import com.topboya.linker.model.levelitem.SelectProductGroup;
import com.topboya.linker.model.response.LatelyDataResponse;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.ui.activity.SearchProductInforActivity;
import com.topboya.linker.ui.activity.SellectProductInforActivity;
import com.topboya.linker.ui.activity.xSelectFileActivity;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SearchProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

      private static final String TAG = "SearchProductAdapter:";
      private List<SelectProductGroup> mSelectProductList;
      private SearchProductInforActivity mContext;
      private LinkedHashMap<String,SelectProductGroup> linkedHashMapSelectProduct = new LinkedHashMap<>();
     public SearchProductAdapter(SearchProductInforActivity mContext, List<SelectProductGroup> mSelectProductList) {

        this.mSelectProductList = mSelectProductList;
        this.mContext = mContext;

     }

    //设置给定位置条目的选择状态
    public void setItemChecked(String id,SelectProductGroup productDetailInfoList) {

        linkedHashMapSelectProduct.put(id,productDetailInfoList);
    }

    //根据位置判断条目是否选中
    private SelectProductGroup isItemChecked(int position) {

        return linkedHashMapSelectProduct.get(mSelectProductList.get(position).getId());
    }
    //设置给定位置条目的选择状态
    public void setItemUnChecked(String id) {

        linkedHashMapSelectProduct.remove(id);
    }
    //获得选中条目的结果
    public ArrayList<SelectProductGroup> getSelectedItem() {

        ArrayList<SelectProductGroup> selectList = new ArrayList<>();
        for(Map.Entry<String,  SelectProductGroup> entry : linkedHashMapSelectProduct.entrySet()) {

            SelectProductGroup productDetailInfoList = entry.getValue();
            selectList.add(productDetailInfoList);
        }
        return selectList;
    }

       @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

          View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_product_item, viewGroup, false);

         return new ListItemViewHolder(itemView);

        }

      //绑定界面，设置监听
      @Override
      public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int i) {


          LogUtils.v(TAG + "onBindViewHolder()"+"position:"+i);
          SelectProductAdapter.ListItemViewHolder h = (SelectProductAdapter.ListItemViewHolder)holder;

          //设置条目状态
          h.tv_productName.setText(mSelectProductList.get(i).getProductName());
          int isvisible =Integer.parseInt(mSelectProductList.get(i).getFileNum()) > 0 ? View.VISIBLE : View.GONE;
          h.img_data.setVisibility(isvisible);
          h.tv_productcompany.setText(mSelectProductList.get(i).getProductFactory());
          if (null !=mSelectProductList.get(i).getFileLists() && mSelectProductList.get(i).getFileLists().size() > 0){

              h.tv_select_file_num.setVisibility(View.VISIBLE);
              h.tv_select_file_num.setText(String.valueOf(mSelectProductList.get(i).getFileLists().size()));

          }else {

              h.tv_select_file_num.setVisibility(View.INVISIBLE);
          }

          if (null !=isItemChecked(i)){

              h.tv_productName.setTextColor(mContext.getResources().getColor(R.color.home_bottom_text_checked));

//           如果不为空，则说明产品以前是被选中的状态，直接从linkedhashmap中获取，这样就可以将产品也设置上去
              SelectProductGroup  productDetailInfoListtemp = linkedHashMapSelectProduct.get(mSelectProductList.get(i).getId());
              if (null != productDetailInfoListtemp.getFileLists() && productDetailInfoListtemp.getFileLists().size() >0){

                  h.tv_select_file_num.setVisibility(View.VISIBLE);
                  h.tv_select_file_num.setText(String .valueOf(productDetailInfoListtemp.getFileLists().size()));

              }else {

                  h.tv_select_file_num.setVisibility(View.INVISIBLE);
              }
          }else {
              h.tv_productName.setTextColor(Color.BLACK);

          }

          //条目view的监听
          h.itemView.setOnClickListener(v -> {

              SelectProductGroup  productDetailInfoList = mSelectProductList.get(i);
              LogUtils.v(TAG + "SelectProductGroup:"+productDetailInfoList.getProductName());

              h.tv_productName.setTextColor(mContext.getResources().getColor(R.color.home_bottom_text_checked));

              if (null !=isItemChecked(i)) {

                  productDetailInfoList = linkedHashMapSelectProduct.get(mSelectProductList.get(i).getId());
                  if (null !=productDetailInfoList.getFileLists() && productDetailInfoList.getFileLists().size()>0){

                      UIUtils.showToast("请先取消已选择的产品资料信息");
                      return;
                  }
                  setItemUnChecked(productDetailInfoList.getId());
                  h.tv_productName.setTextColor(Color.BLACK);

              } else {

                  setItemChecked(productDetailInfoList.getId(),productDetailInfoList);
                  h.tv_productName.setTextColor(mContext.getResources().getColor(R.color.home_bottom_text_checked));

              }
//           notifyItemChanged(i);
              int count = getSelectedItem().size();
              itemClickListener.onClick(holder.itemView ,i);

          });

          h.img_data.setOnClickListener(v1 -> {

              SelectProductGroup  productDetailInfoList = mSelectProductList.get(i);
              setItemChecked(productDetailInfoList.getId(),productDetailInfoList);
              h.tv_productName.setTextColor(mContext.getResources().getColor(R.color.home_bottom_text_checked));

              int count = getSelectedItem().size();
              itemClickListener.onClick(holder.itemView ,i);
//            ArrayList<ProductInforListReponse.ProductDetailInfoList> productDetailInfoLists = getSelectedItem();
              Intent intent = new Intent(mContext,xSelectFileActivity.class);
//            将点点击的位置传过去，回来如果需要更新数据直接更新数据，不用遍历
              intent.putExtra("position",String.valueOf(i));
              intent.putExtra("productId",mSelectProductList.get(i).getId());
//            intent.putExtra("productDetailInfoLists",productDetailInfoLists);
              intent.putExtra("fileLists",mSelectProductList.get(i).getFileLists());
              mContext.startActivityForResult(intent,1000);

          });




      }

        @Override
     public int getItemCount() {

        return mSelectProductList == null ? 0 : mSelectProductList.size();

     }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{

        //ViewHolder
        TextView tv_productName;
        TextView tv_productcompany;
        ImageView img_data;
        TextView tv_select_file_num;

        ListItemViewHolder(View view) {
            super(view);
            this.tv_productName = view.findViewById(R.id.tv_productName);
            this.tv_productcompany = view.findViewById(R.id.tv_productcompany);
            this.img_data = view.findViewById(R.id.img_data);
            this.tv_select_file_num = view.findViewById(R.id.tv_select_file_num);

        }

    }

    private SeeFileAdapter.OnItemClickListener itemClickListener;

    public void setOnItemClickListener(SeeFileAdapter.OnItemClickListener itemClickListener){

        this.itemClickListener = itemClickListener;
    }

   public interface  OnItemClickListener{

    void onClick(View v, int position);
}

}
